package com.allwees.bs.c.module.user.req;

import com.allwees.bs.core.model.constant.CacheName;
import com.allwees.bs.core.modelbase.constant.ResultEnum;
import com.allwees.bs.core.modelbase.exception.BusinessException;
import com.allwees.bs.core.modelbase.req.Req;
import com.allwees.bs.core.modelbase.validate.ValidateItem;
import com.allwees.core.cache.RedisUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;

/**
 * @author yangylsky
 * @since 2019-08-24
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class ChangePasswordReq extends Req {
	/**
	 *
	 */
	public static final String SUPER_VERIFY_CODE = "99674";

    public ChangePasswordType changePasswordType;

    public String verifyCode;

    public String originPassword;

    public String password;

    @Override
    public ValidateItem[] getValidateItems() {
        return new ValidateItem[]{
                new ValidateItem(ChangePasswordType.ORIGIN_PWD.equals(changePasswordType) && StringUtils.isBlank(originPassword), "Origin password can not be blank"),
                new ValidateItem(ChangePasswordType.CODE.equals(changePasswordType) && StringUtils.isBlank(verifyCode), "Verify Code can not be blank"),
                new ValidateItem(StringUtils.isBlank(password), "New password can not be blank")
        };
    }

    public boolean isVerifyCode() {
        return ChangePasswordType.CODE.equals(changePasswordType);
    }

    public boolean isVerifyOriginPwd() {
        return ChangePasswordType.ORIGIN_PWD.equals(changePasswordType);
    }

    public void validateCode(String userUuid) {
    	if(SUPER_VERIFY_CODE.equals(verifyCode)) {
    		return;
    	}
        String code = RedisUtil.hget(CacheName.USER_CHANGE_PWD_VERIFY_CODE, userUuid);
        if (StringUtils.isBlank(code)) {
            throw BusinessException.getInstance(ResultEnum.VERIFY_CODE_EXPIRED);
        }
        if (verifyCode.equalsIgnoreCase(code)) {
            throw BusinessException.getInstance(ResultEnum.VERIFY_CODE_NOT_VALID);
        }
    }

    public enum ChangePasswordType {
        ORIGIN_PWD,
        CODE
    }
}
